package org.example.cet.demos.controller;

import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;

import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.form.AudioForm;
import org.example.cet.demos.domain.po.Audio;
import org.example.cet.demos.service.impl.AudioServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/audio")
public class AudioController {
    @Autowired
    private AudioServiceImpl audioService;
    @Autowired
    private converter converter;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody AudioForm audioForm) {
        Audio audio = converter.ToAudio(audioForm);
        audioService.save(audio);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        audioService.removeById(id);
        return Result.success();
    }

    @GetMapping("/getList")
    public Result getList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer pageSize) {
        PageBean pageBean = audioService.getList(page, pageSize);
        return Result.success(pageBean);
    }

    @GetMapping("/findByTitle/{audioTitle}")
    public Result findByTitle(@PathVariable String audioTitle) {
        return Result.success(audioService.findByTitle(audioTitle));
    }
}

